#!/usr/bin/perl
use strict;

# 请修改log文件目录
my $DIR_LOG = "/opt/apm/analyze/dist/logs";
my $RENAME_FILE="report_data.log";
my $file_log= "./list_log.txt";
my $i_max=60;

my $str_cmd_info="";
my $lineFile;
my $i_idx=0;

my $i_position;
my $str_filename;

my ($i_sec,$i_min,$i_hour,$i_mday,$i_mon,$i_year,$i_wday,$i_yday,$i_isdst) = localtime();
$i_year += 1900;
my $str_renameFilename = sprintf("bak_%s_%d%02d%02d_%02d%02d", $RENAME_FILE, $i_year,$i_mon,$i_mday,$i_hour,$i_min);

#删除老数据包
system("rm -f $file_log;");
#system( qq(ls -lt $DIR_LOG/report*.log | awk '{print $9}' | grep -v '^$' > $file_log) );
system( qq(ls -lt --time-style="+%Y-%m-%d %H:%M:%S" $DIR_LOG/bak_* > $file_log ) );

open(FILE,"<", $file_log)||die"cannot open the file[$file_log]: $!\n";
my @listFile=<FILE>;
foreach $lineFile(@listFile)
{
	$i_idx += 1;
	if( $i_idx <= $i_max) { next; }
	
	$lineFile =~ s/^\s+//;
	$lineFile =~ s/\s+$//;
	
	$i_position = index($lineFile, $DIR_LOG);
	if($i_position == -1) { next; }
	$str_filename = substr($lineFile, $i_position);
	
	$str_cmd_info = sprintf("rm -f \"%s\"", $str_filename);
	printf("cmd[%s]\n", $str_cmd_info );
	system( "$str_cmd_info" );
}

#修改当前log文件
$str_cmd_info = sprintf("mv \"%s/%s\" \"%s/%s\" >/dev/null 2>&1", $DIR_LOG, $RENAME_FILE, $DIR_LOG, $str_renameFilename);
#printf("%s\n", $str_cmd_info);
system( "$str_cmd_info" );

#清理数据
close FILE;
system("rm -f $file_log");

